package com.kaver.modular.dc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaver.core.common.constant.state.ChangeType;
import com.kaver.modular.dc.service.IDcAppParentService;
import com.kaver.modular.dc.service.IDcConfigVersionService;
import com.kaver.modular.system.dao.DcAppParentMapper;
import com.kaver.modular.system.model.DcAppParent;
import com.kaver.modular.system.model.DcConfigVersion;

/**
 * <p>
 * 项目关联 服务实现类
 * </p>
 *
 * @author Roc
 * @since 2019-07-24
 */
@Service
public class DcAppParentServiceImpl extends ServiceImpl<DcAppParentMapper, DcAppParent> implements IDcAppParentService {
	@Autowired
	private IDcConfigVersionService dcConfigVersionService;
	
	@Override
	public List<DcConfigVersion> getAllParentConfig(Integer appId,Integer envId) {
		List<DcConfigVersion> versions = new ArrayList<DcConfigVersion>();
		DcAppParent entity = new DcAppParent();
		entity.setAppId(appId);
		entity.setEnvId(envId);
		Wrapper<DcAppParent> wrapper = new EntityWrapper<DcAppParent>(entity);
		wrapper.orderBy("id", true);
		List<DcAppParent> list = this.selectList(wrapper);
		if (!CollectionUtils.isEmpty(list)) {
			for (DcAppParent appParent : list) {
				Long maxVersion = dcConfigVersionService.maxVersion(appParent.getParentAppId(), envId);
				if (maxVersion != null) {
					DcConfigVersion configVersion = new DcConfigVersion();
					configVersion.setAppId(appParent.getParentAppId());
					configVersion.setEnvId(envId);
					configVersion.setVersion(maxVersion);
					Wrapper<DcConfigVersion> dcwrapper = new EntityWrapper<DcConfigVersion>(configVersion);
					dcwrapper.ne("change_type", ChangeType.DEL.getCode());
					List<DcConfigVersion> configVersions = dcConfigVersionService.selectList(dcwrapper);
					versions.addAll(configVersions);
				}
			}
		}
		return versions;
	}

}
